import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

export default defineConfig({
	plugins: [vue()],
	css: {
		preprocessorOptions: {
			scss: {
				additionalData: `@use "@/styles/variables.scss" as *;`,
			},
		},
	},
	resolve: {
		alias: {
			'@': path.resolve(__dirname, 'src'),
		},
	},
	server: {
		proxy: {
			// 代理所有以 /api 开头的请求到后端，保持完整路径
			'/api': {
				target: 'http://127.0.0.1:8080',
				changeOrigin: true,
				secure: false,
				// 不重写路径，保持/api前缀，因为后端接口就是/api/v1/xxx
				configure: (proxy, options) => {
					proxy.on('proxyReq', (proxyReq, req, res) => {
						console.log('代理请求:', req.method, req.url, '-> ', options.target + req.url)
					})
					proxy.on('proxyRes', (proxyRes, req, res) => {
						console.log('代理响应:', proxyRes.statusCode, req.url)
					})
					proxy.on('error', (err, req, res) => {
						console.log('代理错误:', err.message)
					})
				},
			},
		},
	},
})
